package com.yyh.dequetest;

import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;

/*公民身份管理系统*/
public class CitizenManagementSystem{
    private Deque<Citizen> citizenDeque;
    private Scanner scanner;
    public CitizenManagementSystem(){
        citizenDeque = new LinkedList<>();
        scanner = new Scanner(System.in);
    }

    public static void main(String[] args) {
        CitizenManagementSystem cms = new CitizenManagementSystem();
        cms.run();
    }
    //显示系统主菜单
    public void showMenu(){
        System.out.println("-------公民身份管理系统-------");
        System.out.println("1. 信息录入");
        System.out.println("2. 信息查询");
        System.out.println("3. 信息修改");
        System.out.println("4. 信息删除");
        System.out.println("5. 退出");
        System.out.print("--请选择--");
    }
    //主运行方法
    public void run(){
        while (true) {
            showMenu();
            String choice = scanner.nextLine().trim();
            switch (choice) {
                case "1":
                    input();
                    break;
                case "2":
                    query();
                    break;
                case "3":
                    modify();
                    break;
                case "4":
                    delete();
                    break;
                case "5":
                    exit();
                    return;
                default:
                    System.out.println("输入错误！请重新输入！");
            }
        }
    }
    //信息录入
    public void input(){
        System.out.println("\n=== 信息录入 ===");
        // 输入姓名
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine().trim();
        if (name.isEmpty()) {
            System.out.println("姓名不能为空！");
            return;
        }
        // 输入身份证号
        System.out.print("请输入身份证号: ");
        String id = scanner.nextLine().trim();
        if (id.isEmpty()) {
            System.out.println("身份证号不能为空！");
            return;
        }

        // 检查身份证号是否已存在
        if (findCitizenById(id) != null) {
            System.out.println("该身份证号已存在，无法重复录入！");
            return;
        }
        // 输入性别
        System.out.print("请输入性别(男/女): ");
        String gender = scanner.nextLine().trim();
        if (!gender.equals("男") && !gender.equals("女")) {
            System.out.println("性别输入无效，请输入'男'或'女'！");
            return;
        }
        // 输入地址
        System.out.print("请输入地址: ");
        String address = scanner.nextLine().trim();
        if (address.isEmpty()) {
            System.out.println("地址不能为空！");
            return;
        }
        // 创建公民对象并添加到Deque尾部
        Citizen newCitizen = new Citizen(name, id, gender, address);
        citizenDeque.addLast(newCitizen); // 使用Deque的addLast方法
        System.out.println("✓ 信息录入成功！");
    }
    //根据身份证号查询公民
    public Citizen findCitizenById(String id){
        //使用迭代器遍历Deque
        Iterator<Citizen> it = citizenDeque.iterator();
        while (it.hasNext()) {
            Citizen citizen = it.next();
            if (citizen.getIdNumber().equals(id)) {
                return citizen;
            }
        }
        return null;
    }
    //信息查询
    public void query(){
        System.out.println("\n=== 信息查询 ===");
        if (citizenDeque.isEmpty()){
            System.out.println("没有录入任何公民信息！");
            return;
        }
        System.out.print("请输入身份证号: ");
        String id = scanner.nextLine().trim();
        if (id.isEmpty()){
            System.out.println("身份证号不能为空！");
            return;
        }
        Citizen citizen = findCitizenById(id);
        if (citizen == null){
            System.out.println("没有找到该公民！");
        } else {
            System.out.println("查询结果: ");
            System.out.println(citizen);
        }
    }
    //信息修改
    public void modify(){
        System.out.println("\n=== 信息修改 ===");
        //修改身份证
        if (citizenDeque.isEmpty()){
            System.out.println("系统中没有公民信息！");
            return;
        }
        System.out.print("请输入要修改的身份证号: ");
        String id = scanner.nextLine().trim();
        if (id.isEmpty()){
            System.out.println("身份证号不能为空！");
            return;
        }
        Citizen citizen = findCitizenById(id);
        if (citizen == null){
            System.out.println("没有找到该公民！");
        }
        System.out.println("当前信息: ");
        System.out.println(citizen);
        System.out.print("请输入新的信息（直接回车保持原值）: ");
        //修改姓名
        System.out.print("新姓名: ");
        String newName = scanner.nextLine().trim();
        if (!newName.isEmpty()){
            citizen.setName(newName);
        }
        //修改性别
        System.out.print("新性别: ");
        String newGender = scanner.nextLine().trim();
        if (!newGender.isEmpty()){
            if (newGender.equals("男") || newGender.equals("女")){
                citizen.setGender(newGender);
            }else {
                System.out.println("性别输入无效，保持原值");
            }
        }
        //修改地址
        System.out.print("新地址: ");
        String newAddress = scanner.nextLine().trim();
        if (!newAddress.isEmpty()){
            citizen.setAddress(newAddress);
        }
        System.out.println("✓ 修改成功！");
    }
    //信息删除
    public void delete(){
        System.out.println("\n=== 信息删除 ===");
        if (citizenDeque.isEmpty()){
            System.out.println("系统中没有公民信息！");
            return;
        }
        System.out.print("请输入要删除的公民身份证号: ");
        String id = scanner.nextLine().trim();
        if (id.isEmpty()){
            System.out.println("身份证号不能为空！");
            return;
        }
        Citizen citizen = findCitizenById(id);
        if (citizen == null){
            System.out.println("没有找到该公民！");
        } else {
            citizenDeque.remove(citizen);
            System.out.println("✓ 删除成功！");
        }
    }
    //退出系统
    public void exit(){
        System.out.println("\n=== 退出系统 ===");
        System.out.println("已退出系统！");
    }
}
